clear all
set more off

do "set_path.do"

***** Fig 3: Government CIP by tenor *****

use "${datadir}cip_all_jun2025.dta", clear

rangestat cip_mean = cip_govt, interval(date -13 0) by(currency tenor)
keep group currency tenor date cip_mean

drop if date < mdy(1,1,2004)

* Fill-in data for missing date so we don't have a median with too few observations
reshape wide cip_mean, i(group tenor date) j(currency) string
reshape long cip_mean, i(group tenor date) j(currency) string
sort group currency tenor date
bys group currency tenor: replace cip_mean = cip_mean[_n-1] if missing(cip_mean) & !missing(cip_mean[_n-1])

* Missing 20y/30y data from Jun-2005 to Apr-2011 for these currencies
drop if inlist(currency, "AUD", "CAD", "NZD")

collapse (median) cip_mean, by(group tenor date)

twoway (tsline cip_mean if group=="g10" & tenor=="3m", cmissing(no) lwidth(medthin) lcolor(midblue)   lpattern(solid) legend(label(1 "3M"))) ///
	   (tsline cip_mean if group=="g10" & tenor=="1y", cmissing(no) lwidth(medthin) lcolor(cranberry) lpattern(solid) legend(label(2 "1Y"))) ///
	   (tsline cip_mean if group=="g10" & tenor=="10y", cmissing(no) lwidth(medthin) lcolor(gold)     lpattern(solid) legend(label(3 "10Y"))) ///
	   (tsline cip_mean if group=="g10" & tenor=="20y", cmissing(no) lwidth(medthin) lcolor(midgreen) lpattern(solid) legend(label(4 "20Y"))) ///	
	   (tsline cip_mean if group=="g10" & tenor=="30y", cmissing(no) lwidth(medthin) lcolor(black)    lpattern(solid) legend(label(5 "30Y"))), ///
		yline(0, lwidth(vthin) lcolor(black) lstyle(solid)) ylabel(-100(50)300,labsize(small) nogrid) xlabel(#12, labsize(small) format(%tdCY) nogrid) ///
		xtitle("") ytitle("Basis Points", size(small)) legend(size(small)) ///
		graphregion(lcolor(none) ilcolor(none) fcolor(white) ifcolor(white)) bgcolor(white) plotregion(lcolor(white)) ///
		subtitle(, size(small) ring(0) pos(1) nobexpand) title("Term Structure of Government CIP Deviations", size(medium)) note("") ///
		legend(region(lstyle(none)) rows(1) position(bottom))
		
graph export "${outdir}figures/cip_term_structure_g10.pdf", replace
